using System;
using System.Diagnostics;
using System.Collections;
using System.Windows.Forms;

using UtilityLibrary.CommandBars;

namespace UtilityLibrary.Collections
{
	/// <summary>
	/// Summary description for ToolbarButtonCollection.
	/// </summary>
	public class ToolBarItemCollection : System.Collections.CollectionBase, IEnumerable
	{
		#region Events	
		public event EventHandler Changed;
		#endregion

		#region Class Variable
		ToolBarEx parentToolBar = null;
		#endregion

		#region Constructors
		public ToolBarItemCollection(ToolBarEx parent)
		{
            parentToolBar = parent;
		}

		public ToolBarItemCollection()
		{
		}

		#endregion
			
		#region Methods
		public void Add(ToolBarItem item)
		{
			if ( parentToolBar != null && parentToolBar.PlaceHolderAdded == true )
				parentToolBar.RemovePlaceHolderToolBarItem();
			
			InnerList.Add(item);
			RaiseChanged();
		}

		public void AddRange(ToolBarItem[] InnerList)
		{
			if ( parentToolBar != null && parentToolBar.PlaceHolderAdded == true )
				parentToolBar.RemovePlaceHolderToolBarItem();

			foreach (ToolBarItem item in InnerList)
				this.InnerList.Add(item);
			RaiseChanged();
		}

		public void Insert(int index, ToolBarItem item)
		{
			if ( parentToolBar != null && parentToolBar.PlaceHolderAdded == true )
				parentToolBar.RemovePlaceHolderToolBarItem();

			InnerList.Insert(index, item);
			RaiseChanged();
		}

	
		public void Remove(ToolBarItem item)
		{
			// Remove the item from the ToolBar first
			int index = IndexOf(item);
            if ( parentToolBar != null )
				parentToolBar.RemoveToolBarItem(index);
			
			if (!InnerList.Contains(item)) return;
            InnerList.Remove(item);
			RaiseChanged();

			// If we don't have any items left
			if ( InnerList.Count == 0 && parentToolBar.PlaceHolderAdded == false )
				parentToolBar.AddPlaceHolderToolBarItem();
		}

		public new void Clear()
		{
					
			// Before wiping out all items from
			// the ToolBarItem collection make sure we also
			// delete the items from the toolBar itself
			for ( int i = 0; i < InnerList.Count; i++ )
			{
				if ( parentToolBar != null )
					parentToolBar.RemoveToolBarItem(i);
				
			}
			// Now from the collection
			InnerList.Clear();

			// Inform listeners to update
			RaiseChanged();

			// We don't have any bands left, add
			// the ToolBar place holder
			if ( InnerList.Count == 0 && parentToolBar.PlaceHolderAdded == false )
				parentToolBar.AddPlaceHolderToolBarItem();

		}

		public bool Contains(ToolBarItem item)
		{
			return InnerList.Contains(item);
		}

		public int IndexOf(ToolBarItem item)
		{
			return InnerList.IndexOf(item);
		}

		public ToolBarItem this[int index]
		{
			get 
			{ 
				return (ToolBarItem)InnerList[index]; 
			}
		}

		internal ToolBarItem this[Keys shortcut]
		{
			get
			{
				foreach (ToolBarItem item in InnerList)
					if ( (item.Shortcut == shortcut) && (item.Enabled) && (item.Visible) )
						return item;
				return null;
			}
		}

		internal ToolBarItem this[char mnemonic]
		{
			get
			{
				foreach (ToolBarItem item in InnerList)
				{
					string text = item.Text;
					if ( text != string.Empty && text != null )
					{				   
						for (int i = 0; i < text.Length; i++)
						{
							if ((text[i] == '&') && (i + 1 < text.Length) && (text[i + 1] != '&'))
								if (mnemonic == Char.ToUpper(text[i + 1]))
									return item;
						}
					}
				}
				return null;
			}
		}
		#endregion
			
		#region Implementation
		void RaiseChanged()
		{
			if (Changed != null) Changed(this, null);
		}
		#endregion
	}
}
